home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gold Medal Software 5
/
Gold Medal Software - Volume 5 (Gold Medal) (1995).iso
/
windows
/
win31
/
cenviw.arj
/
PMITEMS.CMM
< prev
next >
Wrap
Text File
|
1994-03-08
|
10KB
|
339 lines
// PMItems.cmm - Utility to make all PM items available from a
// ver.1 list made available at any time from a tiny button
// maintained at top level on the screen.
//
// This uses DDE for communicating with the program
// manager, and uses Windows function for buttons
// and an item list.
#include <Progman.lib>
#include <Window.lib>
#include <WinTools.lib>
#include <Message.lib>
#include <WinExec.lib>
#include <Profile.lib>
#include <MsgBox.lib>
#define BUTTON_WIDTH 10
#define BUTTON_HEIGHT 15
#define BUTTON_ROW 0
#define BUTTON_COL 0
#define BUTTON_TEXT "i"
ItemListCount;
ItemList; // this will be initialized to contain an array of structures with
// these elements:
// .Description - What is displayed in list to select from
// .CommandLine - Statement to begin execution
// .WorkingDirectory - Directory to execute from
// .RunMinimized - Boolean on whethter to minimzed to start
main()
{
ChangeToWindowsDefaultDirectory();
SetWindowTitle(ScreenHandle(),"Building list...");
BuildItemList();
SetWindowTitle(ScreenHandle(),"Sorting list...");
SortItemList();
SetWindowTitle(ScreenHandle(),"Making List Windows...");
ButtonHwnd = MakeItemButton();
MakeItemListbox(ButtonHwnd);
SetWindowTitle(ScreenHandle(),"PMItems");
// hide little icon, for noone wants to see it
ShowWindow(ScreenHandle(),SW_HIDE);
// Keep windows on top; if version 3.1 or higher then just use the on_top
// flags, else move it to top once per few seconds
if ( GetWindowsVersion() < 0x030A ) {
// old windows version, must draw to top every few seconds (approx.)
#define ONTOP_FREQ 3 // seconds between placing on top
NextOnTopTime = 0;
while ( DoWindows(True) ) {
if ( NextOnTopTime <= clock() ) {
MoveWindowToTop3_0(ButtonHwnd);
NextOnTopTime = clock() + CLOCKS_PER_SEC * ONTOP_FREQ;
}
}
} else {
// new windows version simply draw on top
MoveWindowToTop3_1(ButtonHwnd);
while ( DoWindows() ) ;
}
}
//*************** BUILD ITEM LIST ****************
BuildItemList()
{
if ( SpawnProgman = !IsProgmanRunning() ) {
// program manager is not executing, and so start it
if ( 0 == spawn(P_NOWAIT,"Progman.exe") )
FatalError("Program Manager could not be started.");
}
ConnectToProgman();
// get list of all the Program Manager groups
if ( !ProgmanGroups(GroupList,GroupCount) )
FatalError("No groups found in Program Manager.");
// for each group, add to ItemList each item in the group
ItemListCount = 0;
for ( group = 0; group < GroupCount; group++ ) {
if ( !ProgmanGroupItems(GroupList[group],GroupInfo,IList) )
FatalError("Error retrieving items from group \"%s\".",GroupList[group]);
for ( item = 0; item < GroupInfo.Count; item++ ) {
ItemList[ItemListCount].Description = IList[item].Description;
ItemList[ItemListCount].CommandLine = IList[item].CommandLine;
ItemList[ItemListCount].WorkingDirectory = IList[item].WorkingDirectory;
ItemList[ItemListCount].RunMinimized = IList[item].RunMinimized;
ItemListCount++;
}
}
CloseProgmanConnection();
if ( SpawnProgman ) {
// Program manager was started here, and so kill it
if ( !ExitProgman(False) ) {
// wouldn't exit itself, and so send the close message
PostMessage(GetWindowHandle("Program Manager"),WM_QUIT,0,0);
}
}
if ( 0 == ItemListCount )
FatalExit("No items were found in Program Manager");
}
//*************** SORT ITEM LIST ***************
SortFunction(Item1,Item2) // sort alphabetically by description
{
return stricmp(Item1.Description,Item2.Description);
}
SortItemList()
{
qsort(ItemList,ItemListCount,"SortFunction");
}
//*************** BUTTON AND BUTTON WINDOW ***************
MakeItemButton()
{
return MakeWindow(NULL,"button","ItemButtonFunction",
BUTTON_TEXT,WS_VISIBLE | BS_PUSHBUTTON | WS_POPUP,
BUTTON_COL,BUTTON_ROW,
BUTTON_WIDTH,BUTTON_HEIGHT,NULL);
}
ItemButtonFunction(hwnd,msg,wparam,lparam)
{
if ( BM_SETSTATE == msg && !wparam ) {
// Button was clicked, so show item list if not already showing
ShowItemListbox();
}
}
//*************** ITEM LIST LISTBOX ***************
#define LISTBOX_COL 0
#define LISTBOX_ROW 0
#define LISTBOX_WIDTH 250
#define LISTBOX_HEIGHT 500
ListHwnd;
MakeItemListbox(ButtonHwnd) // create listbox hidden
{
ListHwnd= MakeWindow(ButtonHwnd,"listbox","ListBoxFunction",
"PM Items",WS_CAPTION | WS_POPUP | WS_VSCROLL | WS_HSCROLL,
LISTBOX_COL,LISTBOX_ROW,LISTBOX_WIDTH,LISTBOX_HEIGHT,NULL);
// fill listbox with all our choices
for ( i = 0; i < ItemListCount; i++ ) {
SendMessage(ListHwnd,LB_ADDSTRING,0,ItemList[i].Description);
}
}
ListBoxFunction(hwnd,msg,wparam,lparam)
{
switch ( msg ) {
case WM_CHAR:
switch ( wparam ) {
case '\r':
ListboxExecuteCurrentSelection();
return 0;
case 0x1B: // escape
HideItemListbox();
return 0;
}
break;
case WM_LBUTTONDBLCLK:
ListboxExecuteCurrentSelection();
break;
case WM_KILLFOCUS:
HideItemListbox()
break;
}
}
ShowItemListbox()
{
ShowWindow(ListHwnd,SW_RESTORE);
}
HideItemListbox()
{
ShowWindow(ListHwnd,SW_HIDE);
}
ListboxExecuteCurrentSelection()
{
HideItemListbox();
SelectionIndex = SendMessage(ListHwnd,LB_GETCURSEL,0,0);
if ( LB_ERR != SelectionIndex ) {
Selection = ItemList[SelectionIndex];
SetCurrentDirectory(Selection.WorkingDirectory);
ExecuteCommandLine(Selection.CommandLine,Selection.WorkingDirectory,Selection.RunMinimized);
}
}
ExecuteCommandLine(pCmd,pDir,pRunMinimized)
{ // pCmd may specify a file with an extension association, in which case
// it would be correct to run that extension program. Also may combine
// directory with Cmd to get full execution name.
strcpy(lCmd,pCmd);
// Use pDir, if necessary and available, to get full path for lCmd
if ( pDir[0] && lCmd[1] != ':' ) {
// Get full path of directory
if ( !(lDir = FullPath(pDir)) ) {
Error("Invalid Working Directory \"%s\"\n",pDir);
return;
}
// add cmd to appropriate end of the path
if ( lCmd[0] == '\\' ) {
// cmd starts at root dir, so only use working dir drive
sprintf(lCmd,"%c:%s",lDir[0],lCmd);
} else {
// append command to full working directory
if ( !strcmp(lDir+1,":\\") ) lDir[2] = 0;
sprintf(lCmd,"%s\\%s",lDir,lCmd);
}
}
// get file name with whatever parameters may follow
if ( lCmdSaveBlank = strchr(pCmd,' ') ) lCmdSaveBlank[0] = 0;
strcpy(lExtension,SplitFileName(pCmd).ext);
if ( lExtension[1] ) {
lAssociation = GetProfileString("Extensions",lExtension+1,"");
if ( lAssociation[0] ) {
// remove beyond first blank or '^' character
if ( lEndSpec = strpbrk(lAssociation," ^\t") )
lEndSpec[0] = 0;
}
}
if ( lCmdSaveBlank ) lCmdSaveBlank[0] = ' ';
if ( defined(lAssociation) && lAssociation[0] )
sprintf(lCmd,"%s %s",lAssociation,lCmd);
ExecResult = WinExec( lCmd, pRunMinimized ? SW_MINIMIZE : SW_SHOWNORMAL );
if ( ExecResult < 32 )
Error("Error %d on call to WinExec() for command:\n\"%s\"",ExecResult,lCmd);
}
//*************** UTILITY FUNCTIONS ***************
FatalError(FormatString/*,arg,arg,arg*/) // display printf-like error message
{
va_start(va_list,FormatString);
vsprintf(msg,FormatString,va_list);
MessageBox(msg,"ERROR!",MB_ICONEXCLAMATION);
va_end(va_list);
exit(EXIT_FAILURE);
}
Error(FormatString/*,arg,arg,arg*/) // display printf-like error message
{
va_start(va_list,FormatString);
vsprintf(msg,FormatString,va_list);
MessageBox(msg,"ERROR!",MB_ICONEXCLAMATION);
va_end(va_list);
}
SetCurrentDirectory(pDir)
{
if ( NULL != (lDir = pDir) && 0 != lDir[0] ) {
if ( ':' == lDir[1] ) {
// set drive
reg.ah = 0xE;
reg.dl = toupper(lDir[0]) - 'A';
interrupt(0x21,reg);
lDir += 2;
}
if ( lDir[0] ) {
// set directory
reg.ah = 0x3B;
reg.ds = segment(lDir), reg.dx = offset(lDir);
interrupt(0x21,reg);
}
}
}
ChangeToWindowsDefaultDirectory()
{
SetCurrentDirectory(GetWindowsDirectory());
}
GetWindowsDirectory()
{
lWinDir[0] = lWinDir[400] = '\0';
lWinDir[DynamicLink("KERNEL","GETWINDOWSDIRECTORY",UWORD16,PASCAL,lWinDir,399)] = 0;
return lWinDir;
}
GetWindowsVersion() // return major version in hi byte, and minor in low
{ // so 3.1 is 0x030A
lVers = DynamicLink("KERNEL","GETVERSION",UWORD16,PASCAL);
return ((lVers & 0xFF00) >> 8) | ((lVers & 0xFF) << 8);
}
MoveWindowToTop3_0(pHwnd)
{
#define HWND_TOP 0
/* SetWindowPos Flags */
#define SWP_NOSIZE 0x0001
#define SWP_NOMOVE 0x0002
#define SWP_NOZORDER 0x0004
#define SWP_NOREDRAW 0x0008
#define SWP_NOACTIVATE 0x0010
#define SWP_DRAWFRAME 0x0020
#define SWP_SHOWWINDOW 0x0040
#define SWP_HIDEWINDOW 0x0080
#define SWP_NOCOPYBITS 0x0100
#define SWP_NOREPOSITION 0x0200
DynamicLink("USER","SETWINDOWPOS",SWORD16,PASCAL,
pHwnd,HWND_TOP,0,0,0,0,
SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE);
}
MoveWindowToTop3_1(pHwnd)
{
#define HWND_TOPMOST -1
DynamicLink("USER","SETWINDOWPOS",SWORD16,PASCAL,
pHwnd,HWND_TOPMOST,0,0,0,0,
SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE);
}